package com.googlecode.gwtbb.client.only.widget.display.picture;

import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.Label;
import com.googlecode.gwtbb.client.only.widget.basic.TooltipMouseListener;
import com.googlecode.gwtbb.client.shared.entity.picture.Picture;
import com.googlecode.gwtbb.client.shared.entity.picture.Thumb;
import com.googlecode.gwtbb.client.shared.util.StringUtils;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Aug 6, 2008
 */
public class TooltipThumb extends Composite {
	private final Picture picture;

	public TooltipThumb(Picture picture) {
		this.picture = picture;
		SimplePanel mainPanel = new SimplePanel();
		initWidget(mainPanel);

		Thumb thumb = picture.getPrimaryThumb();
		PropImage propImage = new PropImage(thumb);
		mainPanel.setWidget(propImage);
		Widget mouseOverWidget = instantiateMouseOverWidget();
		if (mouseOverWidget != null)
			propImage.getImage().addMouseListener(new TooltipMouseListener(mouseOverWidget));
	}

	public Picture getPicture() {
		return picture;
	}

	public Widget instantiateMouseOverWidget() {
		Widget retVal = null;
		String mouseOverText = getMouseOverText();
		if (StringUtils.isNotEmpty(mouseOverText))
			retVal = new Label(mouseOverText);
		return retVal;
	}

	public String getMouseOverText() {
		return getPicture().getCaption();
	}
}
